package com.mall.malllibrary.net

import android.content.Context
import com.mall.malllibrary.net.callback.IComplete
import com.mall.malllibrary.net.callback.IError
import com.mall.malllibrary.net.callback.IFailure
import com.mall.malllibrary.net.callback.IRequest
import com.mall.malllibrary.net.callback.ISuccess
import com.mall.malllibrary.ui.loader.LoaderStyle
import java.util.WeakHashMap

// 构建restClient并初始化和回调

// z注意这里的简单的主构造方法的使用
class RestClientBuilder (
    private var url:String? = null,
    private var request:IRequest? = null,
    private var success:ISuccess? = null,
    private var failure:IFailure? = null,
    private var error:IError? = null,
    private var complete:IComplete? = null,
    private var context: Context? = null,
    private  var loaderStyle: LoaderStyle? = null,
){

    private val mParams = WeakHashMap<String, Any>()

    fun url(url:String):RestClientBuilder{
        this.url = url
        return this
    }

    fun params(key:String, value: Any):RestClientBuilder{
        mParams[key] = value
        return this
    }

    fun params(param:WeakHashMap<String, Any>):RestClientBuilder{
        mParams.putAll(param)
        return this
    }

    fun request(request : IRequest):RestClientBuilder{
        this.request = request
        return this
    }
    fun success(success : ISuccess):RestClientBuilder{
        this.success = success
        return this
    }
    fun complete(complete : IComplete):RestClientBuilder{
        this.complete = complete
        return this
    }
    fun error(error : IError):RestClientBuilder{
        this.error = error
        return this
    }
    fun failure(failure : IFailure):RestClientBuilder{
        this.failure = failure
        return this
    }
    fun loader(context: Context , style: LoaderStyle):RestClientBuilder{
        this.context = context
        this.loaderStyle = style
        return this
    }

    fun loader(context: Context ) :RestClientBuilder{
        this.context = context
        this.loaderStyle = LoaderStyle.BallClipRotateMultipleIndicator
        return this
    }

    fun build():RestClient{
        return RestClient(url, request, success, failure, error, complete, mParams, context, loaderStyle)
    }

}